package com.example.administrator.chesstest01;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyService extends Service {
    private String TAG = "MyData";
    private boolean isClose = false;
    private PrintWriter pw = null;
    private OptBinder binder = new OptBinder();
    private Handler handler = null;
    private ExecutorService es = Executors.newFixedThreadPool(6);
    public MyService() {
    }

    public class OptBinder extends Binder {
        public void sendData(final String data){
            SendDataThread sendDataThread = new SendDataThread(pw,data);
            es.execute(sendDataThread);
        }
        public void setHandler(Handler handler){
            MyService.this.handler = handler;
        }
    }
    @Override
    public void onCreate() {
        super.onCreate();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Socket client = new Socket("192.168.3.232",54321);
                    InputStream is = client.getInputStream();
                    OutputStream os = client.getOutputStream();
                    InputStreamReader isr = new InputStreamReader(is,"utf-8");
                    BufferedReader br = new BufferedReader(isr);
                    OutputStreamWriter osw = new OutputStreamWriter(os,"utf-8");
                    BufferedWriter bw = new BufferedWriter(osw);
                    pw = new PrintWriter(bw);
                    //阻塞在while处一直读
                    while(!isClose){
                        String data = br.readLine();
                        if (null == data){
                            break;
                        }
                        Log.d(TAG,data);
                        JSONObject jo = new JSONObject(data);
                        int cmd = jo.getInt("cmd");
                        Log.d(TAG,"cmd = "+cmd);
                        Message msg = new Message();
//                        if (null != handler){
                            switch (cmd){
                                case Common.JsonCmdResp.CREATE_ROOM:
                                    msg.what = Common.JsonCmdResp.CREATE_ROOM;
                                    break;
                                case Common.JsonCmdResp.APPLY_FOR_ROOM_LIST:
                                    msg.what = Common.JsonCmdResp.APPLY_FOR_ROOM_LIST;
                                    break;
                                case Common.JsonCmdResp.APPLY_FOR_JOIN_ROOM:
                                    msg.what = Common.JsonCmdResp.APPLY_FOR_JOIN_ROOM;
                                    break;
                                case Common.JsonCmdResp.READY:
                                    msg.what = Common.JsonCmdResp.READY;
                                    break;
                                case Common.JsonCmdReq.RECEIVE_CHESS:
                                    msg.what = Common.JsonCmdReq.RECEIVE_CHESS;
                                    break;
                                case Common.JsonCmdResp.OPPO_PLAYER_EXIT_ROOM:
                                    msg.what = Common.JsonCmdResp.OPPO_PLAYER_EXIT_ROOM;
                                default:
                                    break;
                            }
                            Log.d(TAG,handler.getMessageName(msg));
                            msg.obj = data;
                            handler.sendMessage(msg);
//                        }
                    }

                    pw.close();
                    pw = null;
                    bw.close();
                    osw.close();
                    os.close();

                    br.close();
                    isr.close();
                    is.close();

                    client.close();
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isClose = true;
    }
}

